package com.ruicar.afs.cloud.afscase.loanapprove.task.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruicar.afs.cloud.afscase.channel.entity.ChannelBaseInfo;
import com.ruicar.afs.cloud.afscase.channel.entity.ChannelRiskInfo;
import com.ruicar.afs.cloud.afscase.channel.service.ChannelBaseInfoService;
import com.ruicar.afs.cloud.afscase.channel.service.impl.ChannelRiskInfoServiceImpl;
import com.ruicar.afs.cloud.afscase.infomanagement.entity.CaseCarInfo;
import com.ruicar.afs.cloud.afscase.infomanagement.entity.CaseChannelInfo;
import com.ruicar.afs.cloud.afscase.infomanagement.service.CaseCarInfoService;
import com.ruicar.afs.cloud.afscase.infomanagement.service.CaseChannelInfoService;
import com.ruicar.afs.cloud.afscase.loanapprove.task.condition.LoanApproveTaskCondition;
import com.ruicar.afs.cloud.afscase.loanapprove.task.service.LoanRepairApproveTaskService;
import com.ruicar.afs.cloud.afscase.loanapprove.task.vo.LoanRepairApproveTaskVO;
import com.ruicar.afs.cloud.common.core.security.util.SecurityUtils;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import com.ruicar.afs.cloud.common.modules.afscorebusiness.enums.LoanProcessTypeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>Description:放款补件审核待办任务相关</p>
 *
 * @author xueyu.ma
 * @Version 1.0
 * @CreateDate 2020-6-11
 */
@Slf4j
@RestController
@AllArgsConstructor
@Api("放款补件审核待办任务")
@RequestMapping("/loanRepairApproveTask")
public class LoanRepairApproveTaskController {

    private final LoanRepairApproveTaskService loanRepairApproveTaskService;
    private final CaseChannelInfoService caseChannelInfoService;
    private final CaseCarInfoService caseCarInfoService;
    private ChannelRiskInfoServiceImpl channelRiskInfoService;
    private ChannelBaseInfoService channelBaseInfoService;

    @PostMapping("/getLoanRepairApproveTaskList")
    @ApiOperation(value = "多条件获取放款补件审核待办任务列表")
    public IResponse<List<LoanRepairApproveTaskVO>> queryLoanRepairApproveTaskList(@RequestBody LoanApproveTaskCondition loanApproveTaskCondition) {
        String userId = SecurityUtils.getUser().getId().toString();
        loanApproveTaskCondition.setApproveStaff(userId);
        //流程类型：补件流程
        List<String> loanWorkflowTypeList = new ArrayList<>();
        loanWorkflowTypeList.add(LoanProcessTypeEnum.LOAN_REPAIR.getCode());
        loanApproveTaskCondition.setProcessTypeList(loanWorkflowTypeList);
        Page page = new Page(loanApproveTaskCondition.getPageNumber(), loanApproveTaskCondition.getPageSize());
        IPage<LoanRepairApproveTaskVO> loanRepairApproveTaskVOList = loanRepairApproveTaskService.getLoanRepairApproveTaskVOList(page, loanApproveTaskCondition);
        loanRepairApproveTaskVOList.getRecords().forEach(loanRepairApproveTaskVO -> {
            /** 案件渠道信息 */
            CaseChannelInfo caseChannelInfo = caseChannelInfoService.getOne(Wrappers.<CaseChannelInfo>query().lambda()
                    .eq(CaseChannelInfo::getApplyNo, loanRepairApproveTaskVO.getApplyNo()));
            loanRepairApproveTaskVO.setCaseChannelInfo(caseChannelInfo);
            if (caseChannelInfo != null) {
                loanRepairApproveTaskVO.setDealerName(caseChannelInfo.getDealerName());
                /**渠道基本信息*/
                ChannelBaseInfo channelBaseInfo = channelBaseInfoService.getOne(Wrappers.<ChannelBaseInfo>query().lambda()
                        .eq(ChannelBaseInfo::getChannelCode, caseChannelInfo.getDealerNo()));
                if (channelBaseInfo != null) {
                    ChannelRiskInfo channelRiskInfo = channelRiskInfoService.getOne(Wrappers.<ChannelRiskInfo>query().lambda()
                            .eq(ChannelRiskInfo::getChannelId, channelBaseInfo.getChannelId()));
                    loanRepairApproveTaskVO.setChannelGrade(channelRiskInfo != null ? channelRiskInfo.getChannelGrade() : "");
                    loanRepairApproveTaskVO.setChannelType(channelBaseInfo.getChannelType());
                }
            }
            /**  渠道风控信息  **/
            /**案件车辆信息*/
            CaseCarInfo caseCarInfo = caseCarInfoService.getOne(Wrappers.<CaseCarInfo>query().lambda()
                    .eq(CaseCarInfo::getApplyNo, loanApproveTaskCondition.getApplyNo()));
            loanRepairApproveTaskVO.setBrandCode(caseCarInfo != null ? caseCarInfo.getBrandCode() : null);
        });
        if (StringUtils.isNotBlank(loanApproveTaskCondition.getDealerName())) {
            List<LoanRepairApproveTaskVO> loanRepairApproveTaskVOResult = loanRepairApproveTaskVOList.getRecords().stream()
                    .filter(LoanRepairApproveTaskVO -> StringUtils.isNotBlank(LoanRepairApproveTaskVO.getDealerName())
                            && LoanRepairApproveTaskVO.getDealerName().indexOf(loanApproveTaskCondition.getDealerName()) >= 0)
                    .collect(Collectors.toList());
            loanRepairApproveTaskVOList.getRecords().clear();
            loanRepairApproveTaskVOList.setRecords(loanRepairApproveTaskVOResult);
        }
        if (StringUtils.isNotBlank(loanApproveTaskCondition.getChannelType())) {
            List<LoanRepairApproveTaskVO> loanApproveTaskVOResult = loanRepairApproveTaskVOList.getRecords().stream()
                    .filter(LoanRepairApproveTaskVO -> loanApproveTaskCondition.getChannelType().equals(LoanRepairApproveTaskVO.getChannelType()))
                    .collect(Collectors.toList());
            loanRepairApproveTaskVOList.getRecords().clear();
            loanRepairApproveTaskVOList.setRecords(loanApproveTaskVOResult);
        }
        long pages = loanRepairApproveTaskVOList.getRecords().size() / loanRepairApproveTaskVOList.getSize();
        if (loanRepairApproveTaskVOList.getRecords().size() % loanRepairApproveTaskVOList.getSize() != 0L) {
            ++pages;
        }
        loanRepairApproveTaskVOList.setTotal(loanRepairApproveTaskVOList.getRecords().size());
        loanRepairApproveTaskVOList.setPages(pages);
        return IResponse.success(loanRepairApproveTaskVOList);
    }
}
